package com.redrose.flutterdemo;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.redrose.flutterdemo.databinding.ActivityMainBinding;
import com.redrose.flutterdemo.flutter.FlutterHandler;
import com.redrose.flutterdemo.rxbus.RxBus;
import com.redrose.flutterdemo.rxbus.event.MenuSwitchEvent;
import com.redrose.flutterdemo.ui.activitys.BaseActivity;
import com.redrose.flutterdemo.ui.fragments.HomeFragment;
import com.redrose.flutterdemo.ui.fragments.MinFragment;
import com.redrose.flutterdemo.ui.fragments.SecondFragment;
import com.redrose.flutterdemo.ui.fragments.ThreeFragment;
import com.redrose.flutterdemo.utils.TabEntity;
import com.redrose.flutterdemo.utils.Utils;

import java.util.ArrayList;

import io.flutter.facade.Flutter;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterView;

@Route(path = "/activity/main")
public class MainActivity extends BaseActivity<ActivityMainBinding> {
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mTitles = {"首页", "活动", "照片", "我的"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_icon_hone_default, R.mipmap.tab_icon_active_default, R.mipmap.tab_icon_photo_default, R.mipmap.tab_icon_mine_default};
    private int[] mIconSelectIds = {
            R.mipmap.tab_icon_hone_pre, R.mipmap.tab_icon_active_pre, R.mipmap.tab_icon_photo_pre, R.mipmap.tab_icon_mine_pre};
    private HomeFragment homeFragment;
    private SecondFragment secondFragment;
    private ThreeFragment threeFragment;
    private MinFragment mineFragment;

    @Override
    public void initData() {

    }

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Utils.getStatusBarHeight(this);
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, false, false);
        hideToolBar();
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        Log.d(TAG, " name ----" + name);
        initTab();
        initFragment(savedInstanceState);
//        initView();
    }


    private void initFragment(Bundle savedInstanceState) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        int currentTabPosition = 0;
        if (savedInstanceState != null) {
            homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag("homeFragment");
            currentTabPosition = savedInstanceState.getInt(Utils.HOME_CURRENT_TAB_POSITION);
        } else {
            homeFragment = new HomeFragment();
            secondFragment = new SecondFragment();
            threeFragment = new ThreeFragment();
            mineFragment = new MinFragment();
            transaction.add(R.id.fl_body, homeFragment, "homeFragment");
            transaction.add(R.id.fl_body, secondFragment, "secondfragment");
            transaction.add(R.id.fl_body, threeFragment, "threefragment");
            transaction.add(R.id.fl_body, mineFragment, "minefragment");
        }
        transaction.commit();
        switchTo(currentTabPosition);
        mBinding.tabLayout.setCurrentTab(currentTabPosition);
    }

    /**
     * 初始化tab
     */
    private void initTab() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mBinding.tabLayout.setTextSelectColor(Color.RED);
        mBinding.tabLayout.setTextUnselectColor(Color.BLACK);
        mBinding.tabLayout.setTabData(mTabEntities);
        mBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchTo(position);
                RxBus.getDefault().post(new MenuSwitchEvent(position));
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    /**
     * 切换
     */
    private void switchTo(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            //首页
            case 0:
                hideShowFragment(transaction, secondFragment, threeFragment, mineFragment, homeFragment);
                Utils.setStatusTextColor(false, MainActivity.this);
                break;
            //活动页
            case 1:
                hideShowFragment(transaction, homeFragment, threeFragment, mineFragment, secondFragment);
                Utils.setStatusTextColor(true, MainActivity.this);
                break;
            //相册
            case 2:
                hideShowFragment(transaction, homeFragment, secondFragment, mineFragment, threeFragment);
                Utils.setStatusTextColor(true, MainActivity.this);
                break;
            //我的
            case 3:
                hideShowFragment(transaction, homeFragment, secondFragment, threeFragment, mineFragment);
                Utils.setStatusTextColor(true, MainActivity.this);
                break;
            default:
                break;
        }

    }

    private void hideShowFragment(FragmentTransaction transaction, Fragment fragment1, Fragment
            fragment2, Fragment fragment3, Fragment fragment4) {
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.show(fragment4);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //奔溃前保存位置
        if (mBinding.tabLayout != null) {
            outState.putInt(Utils.HOME_CURRENT_TAB_POSITION, mBinding.tabLayout.getCurrentTab());
        }
    }
}
